package com.frxuexi.park2209.park

import android.app.AlertDialog
import android.content.DialogInterface
import android.content.DialogInterface.OnClickListener
import android.content.Intent
import android.graphics.BitmapFactory
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.ParkList
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209.R
import com.frxuexi.park2209.adapter.ParkAdapter
import com.frxuexi.park2209.databinding.ActivityParkBinding
import com.google.gson.Gson
import com.google.vr.sdk.widgets.pano.VrPanoramaView
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {
    lateinit var parkAdapter: ParkAdapter
    var position=-1
    override fun initData() {
        lifecycleScope.launch {
            vm.parksSuccess.collect{
                if(!it.isNullOrEmpty()) {
                    val parkList = Gson().fromJson(it, ParkList::class.java)
                    Log.i("===", parkList.toString())
                    parkAdapter.addData(parkList.sortedByDescending { it.parkId })
                    parkAdapter.notifyDataSetChanged()
                }
            }
        }

        lifecycleScope.launch {
            vm.parksSucRoom.observe(this@ParkActivity,{
                    parkAdapter.addData(it)
                    parkAdapter.notifyDataSetChanged()
            })
        }

        lifecycleScope.launch {
            vm.deleteParkSuccess.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong("删除成功")
                    parkAdapter.remove(position)
                }
            }
        }

        lifecycleScope.launch {
            vm.updateParkSuccess.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong(it)
                }
            }
        }

        lifecycleScope.launch {
            vm.updateParkSucRoom.collect{
                if(it>0){
                    ToastUtils.showLong(it)
                    parkAdapter.data.clear()
                    vm.getParks()
                }
            }
        }

    }

    override fun initView() {
        binding.parkHeader.apply {
            ivBack.setOnClickListener {
                finish()
            }
            headerTitle.text="车位管理"
        }
        parkAdapter= ParkAdapter()
        binding.rvPark.apply {
            adapter=parkAdapter
            layoutManager=GridLayoutManager(this@ParkActivity,2)
        }
        parkAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@ParkActivity,ParkVRActivity::class.java))
        }

        parkAdapter.setOnItemLongClickListener { adapter, view, position ->
            val parkId = parkAdapter.data.get(position).parkId
            this.position=position
            AlertDialog.Builder(this@ParkActivity)
                .setTitle("删除提醒")
                .setMessage("是否删除${parkId}吗？")
                .setPositiveButton("确定",object :OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                       vm.deletePark(mapOf("parkId" to parkId!!))
                    }

                }).setNegativeButton("取消",null)
                .show()
            return@setOnItemLongClickListener true
        }

        parkAdapter.addChildClickViewIds(R.id.park_updata)
        parkAdapter.setOnItemChildClickListener { adapter, view, position ->
//            vm.updatePark(mapOf("newParkId" to parkAdapter.data.get(position).parkId,
//                "parkName" to "aa"))
//            parkAdapter.data.get(position).parkName="aa"
//            parkAdapter.notifyItemChanged(position)
            val item = parkAdapter.data.get(position)
            item.parkName="ff"
            vm.updatePark(item)
        }
    }

    override fun onResume() {
        super.onResume()
        parkAdapter.data.clear()
        vm.getAllParks()
//        vm.getParks()
    }
}